<?php

namespace app\admin\model\xilujob;

use think\Cache;
use think\db\Query;
use think\Model;

/**
 * 地区数据模型
 */
class Area extends Model
{
    protected $name = 'xilujob_area';

    protected $append = [

    ];

    public function getLevelList()
    {
        return ['1' => __('Level 1'), '2' => __('Level 2'), '3' => __('Level 3'), '4' => __('Level 4')];
    }

    public function getIsReList()
    {
        return ['0' => __('Is_re 0'), '1' => __('Is_re 1')];
    }

    public function getStatusList()
    {
        return ['1' => __('Status 1'), '0' => __('Status 0')];
    }


    public function getLevelTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['level']) ? $data['level'] : '');
        $list = $this->getLevelList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getIsReTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_re']) ? $data['is_re'] : '');
        $list = $this->getIsReList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }



    /**
     * 根据经纬度获取当前地区信息
     *
     * @param string $lng 经度
     * @param string $lat 纬度
     * @return Area 城市信息
     */
    public static function getAreaFromLngLat($lng, $lat, $level = 3)
    {
        $namearr = [1 => 'geo:province', 2 => 'geo:city', 3 => 'geo:district'];
        $rangearr = [1 => 15000, 2 => 1000, 3 => 200];
        $geoname = isset($namearr[$level]) ? $namearr[$level] : $namearr[3];
        $georange = isset($rangearr[$level]) ? $rangearr[$level] : $rangearr[3];
        // 读取范围内的ID
        $redis = Cache::store('redis')->handler();
        $georadiuslist = [];
        if (method_exists($redis, 'georadius')) {
            $georadiuslist = $redis->georadius($geoname, $lng, $lat, $georange, 'km', ['WITHDIST', 'COUNT' => 5, 'ASC']);
        }

        if ($georadiuslist) {
            list($id, $distance) = $georadiuslist[0];
        }
        $id = isset($id) && $id ? $id : 3;
        return self::get($id);
    }

    /**
     * 根据经纬度获取省份
     *
     * @param string $lng 经度
     * @param string $lat 纬度
     * @return Area
     */
    public static function getProvinceFromLngLat($lng, $lat)
    {
        $provincedata = null;
        $citydata = self::getCityFromLngLat($lng, $lat);
        if ($citydata) {
            $provincedata = self::get($citydata['pid']);
        }
        return $provincedata;
    }

    /**
     * 根据经纬度获取城市
     *
     * @param string $lng 经度
     * @param string $lat 纬度
     * @return Area
     */
    public static function getCityFromLngLat($lng, $lat)
    {
        $citydata = null;
        $districtdata = self::getDistrictFromLngLat($lng, $lat);
        if ($districtdata) {
            $citydata = self::get($districtdata['pid']);
        }
        return $citydata;
    }

    /**
     * 根据经纬度获取地区
     *
     * @param string $lng 经度
     * @param string $lat 纬度
     * @return Area
     */
    public static function getDistrictFromLngLat($lng, $lat)
    {
        $districtdata = self::getAreaFromLngLat($lng, $lat, 3);
        return $districtdata;
    }

    public static function getProvinceAndCity($useCache=true) {
        $cacheKey = 'province_city';
        $data = $useCache ? Cache::get($cacheKey) : null;
        if(!$data) {
            $data = self::where('level', 1)
                ->with(['children' => function (Query $query) {
                    $query->field('id,pid,name');
                }])
                ->order('weigh desc')
                ->field('id,name')
                ->select();
            Cache::set($cacheKey, $data, 3600);
        }
        return $data;
    }

    public static function getDistrictsWith($cityId, $useCache=true) {
        $cacheKey = 'districts_with_'.$cityId;
        $data = $useCache ? Cache::get($cacheKey) : null;
        if(!$data) {
            $data = self::where('level', 3)->where('pid', $cityId)
                ->with(['children' => function (Query $query) {
                    $query->field('id,pid,name');
                }])
                ->order('weigh desc')
                ->field('id,name')
                ->select();
            Cache::set($cacheKey, $data, 3600);
        }
        return $data;
    }

    public function children() {
        return $this->hasMany('Area', 'pid', 'id');
    }

}
